<template>
  <div class="">
    <el-breadcrumb>
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item v-breadcrumb-certify></el-breadcrumb-item>
    </el-breadcrumb>
    <div class="content">
      <e-cellbox middle class="coach-manage">
        <e-cell-item>
          <e-heading grade='1'>预览合同</e-heading>
        </e-cell-item>
      </e-cellbox>
      <div>
        <div id="loading" v-if="!previewImg">
          <img src="/static/images/sponsor/timg.gif"/>
          <p>合同生成中...</p>
        </div>
        <img v-else :src="previewImg" class="previewImg"/>
        <div class="button-box compactShowBtn">
          <el-button type="primary" @click="handleDownload" :disabled="!previewImg">合同下载</el-button>
          <el-button :disabled="!previewImg" @click="handleReturn">返回</el-button>
        </div>
      </div>  
    </div>
  </div>
</template>

<script>
import api from 'api/merchants'
import { downloadFile } from 'utils'
export default {
  data () {
    return {
      contractId: this.$route.params.contractId,
      previewImg: '',
      fileName: '',
      bidId: ''
    }
  },
  created () {
    this.$store.commit('LAYOUT_GRID', false)
    api.contract_list_preview_show({contractId: this.contractId}, {context: this, successMsg: 'none'}).then((json) => {
      this.previewImg = 'data:image/png;base64,' + json.data.imgData
      this.fileName = json.data.fileName
      this.merId = json.data.merId
    }).catch(() => {
      this.$router.go(-1)
    })
  },
  destroyed () {
    this.$store.commit('LAYOUT_GRID', true)
  },
  methods: {
    handleReturn () {
      this.$router.push({path: '/sponsor/merchants/compact/manage/' + this.merId})
    },
    handleDownload () { // 合同下载
      let url = `/api/sponsored/org/contract/preview/download?contractId=${this.contractId}`
      downloadFile(url, this.fileName + '.pdf')
    }
  }
}
</script>

<style lang="scss" scoped>
  
  .previewImg{
    width: 100%;
  }
  #loading{
    margin: 40px 0 100px 0;
    text-align: center;
    p{
      font-size: 16px;
    }
  }
  .compactShowBtn{
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: #FFFFFF;
    border-top: 1px solid $border-color-base;
    padding: 30px 0;
    margin-bottom: 0;
  }
</style>
